package com.example.campus.controller.admin;

import com.example.campus.pojo.dto.CategoryAddDTO;
import com.example.campus.pojo.dto.CategoryPageQueryDTO;
import com.example.campus.pojo.entity.Category;
import com.example.campus.result.PageResult;
import com.example.campus.result.Result;
import com.example.campus.service.CategoryService;
import com.example.campus.utils.constant.MessageConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "管理端分类相关接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    @ApiOperation("获得分类列表")
    public Result<PageResult> categoryList(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageResult pageResult = categoryService.getCategoryList(categoryPageQueryDTO);
        return Result.success(pageResult, MessageConstant.GET_LIST_SUCCESS);
    }

    @PostMapping("/add")
    @ApiOperation("添加分类")
    public Result addCategory(@RequestBody CategoryAddDTO categoryAddDTO) {
        categoryService.addCategory(categoryAddDTO);
        return Result.success(MessageConstant.ADD_SUCCESS);
    }

    @PutMapping("/update")
    @ApiOperation("更新分类")
    public Result updateCategory(@RequestBody Category category) {
        categoryService.updateCategory(category);
        return Result.success(MessageConstant.UPDATE_SUCCESS);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除分类")
    public Result deleteCategory(@PathVariable Long id) {
        categoryService.deleteCategory(id);
        return Result.success(MessageConstant.DELETE_SUCCESS);
    }
}
